package com.chongchong.message.client;


import com.chongchong.common.enums.ApiErrorCode;
import com.chongchong.common.tool.ApiNetworkException;
import com.chongchong.common.tool.BaseHttpClient;
import com.chongchong.common.tool.GsonUtil;
import com.chongchong.message.entity.request.SendMessageRequest;
import com.chongchong.message.entity.response.AppTokenResponse;
import com.chongchong.core.tool.jackson.JsonUtil;
import com.chongchong.message.entity.request.AppTokenRequest;
import com.squareup.okhttp.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * <h4>springcloud-activiti7</h4>
 * <p></p>
 *
 * @author : tlp
 * @date : 2022-07-26 09:36
 **/
@Slf4j
@Component
public class MessageClient {

	@Value("${message.config.accessTokenUrl}")
	private String accessTokenUrl;

	@Value("${message.config.corpid}")
	private String corpid;

	@Value("${message.config.corpsecret}")
	private String corpsecret;

	@Value("${message.config.appid}")
	private String appid;

	@Value("${message.config.sendUrl}")
	private String sendUrl;


	/**
	 * 请求用户授权Token
	 *
	 * @param request
	 * @return
	 */
	public AppTokenResponse appToken(AppTokenRequest request) {
		try {

			log.info("请求应用授权接口-调用接口路径:url={},调用接口入参:request={}", accessTokenUrl, JsonUtil.toJson(request));
			Map<String, String> params = new HashMap<>();
			params.put("corpid", corpid);
			params.put("corpsecret", corpsecret);
			params.put("appid", appid);
			Response response = BaseHttpClient.getForEntity(accessTokenUrl, params, Response.class);
			String body = null;
			try {
				body = response.body().string();
				log.info("请求应用授权接口,返回数据：body={}", body);
			} catch (Exception e) {
				log.error("请求应用授权接口-获取返回的数据失败");
				log.error(e.getMessage(), e);
				throw new ApiNetworkException(ApiErrorCode.UNKNOWN_ERROR);
			}
			int code = response.code();
			if (HttpStatus.SC_OK != code) {
				throw new ApiNetworkException(String.valueOf(code), response.message());
			}
			AppTokenResponse res = GsonUtil.getInstance().fromJson(body, AppTokenResponse.class);
			log.info("请求应用授权接口-结果:result={}", response);
			return res;
		} catch (Exception e) {
			log.error("请求应用授权接口失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.UNKNOWN_ERROR);
		}
	}

	/**
	 * 发送待办消息
	 * @param request
	 * @param token
	 */
	public void sendToDoMessage(SendMessageRequest request, String token) {
		try {
			log.info("请求发送待办消息接口-调用接口路径:url={},调用接口入参:request={}", sendUrl, JsonUtil.toJson(request));

			Response response = BaseHttpClient.post(sendUrl + "?app_access_token=" + token, request, Response.class);
			String body = null;
			try {
				body = response.body().string();
				log.info("请求发送待办消息接口,返回数据：body={}", body);
			} catch (Exception e) {
				log.error("请求发送待办消息接口-获取返回的数据失败");
				log.error(e.getMessage(), e);
				throw new ApiNetworkException(ApiErrorCode.UNKNOWN_ERROR);
			}
			int code = response.code();
			if (HttpStatus.SC_OK != code) {
				throw new ApiNetworkException(String.valueOf(code), response.message());
			}
			log.info("请求发送待办消息接口-结果:result={}", response);
		} catch (Exception e) {
			log.error("请求发送待办消息失败");
			log.error(e.getMessage(), e);
			throw new ApiNetworkException(ApiErrorCode.UNKNOWN_ERROR);
		}
	}


}
